package com.spring.authManage.controller;

import com.spring.authManage.pojo.ResourceInfo;
import com.spring.authManage.pojo.ResourceInfoParam;
import com.spring.authManage.pojo.RoleInfo;
import com.spring.authManage.service.ResourceInfoService;
import com.spring.authManage.service.RoleInfoService;

import com.spring.common.util.ChunQiuTool;
import com.spring.common.util.JsonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.google.common.base.Strings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("roleInfoController")
@RequestMapping("/roleInfo")
public class RoleInfoController {
	@Autowired
	private RoleInfoService roleInfoService;
	@Autowired
	private ResourceInfoService resourceInfoService;

	/**
	 * 进入角色管理主页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/toRoleInfoPage.do")
	public ModelAndView toRoleInfoPage(HttpServletRequest request,
			HttpServletResponse response, RoleInfo roleInfo) throws Exception {
		roleInfo.init(request);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		RoleInfo info = this.roleInfoService.queryRoles(roleInfo);
		resultMap.put("pageResult", info.pageResult);
		return new ModelAndView("authManage/roleManage", resultMap);
	}

	/**
	 * 分页查询角色
	 * 
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryRoles.do")
	public ModelAndView queryRoles(HttpServletRequest request,
			HttpServletResponse response, RoleInfo roleInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		roleInfo.init(request);
		RoleInfo info = roleInfoService.queryRoles(roleInfo);
		resultMap.put("pageResult", info.pageResult);
		return new ModelAndView("authManage/roleManage", resultMap);
	}
	/**
	 * 增改角色页面
	 * 
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toRoleInfoEditPage.do")
	public ModelAndView toRoleInfoEditPage(HttpServletRequest request,
			HttpServletResponse response, RoleInfo roleInfo) throws Exception {
		request.setCharacterEncoding("UTF-8");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String role_id = ChunQiuTool.convertNull(roleInfo.getId());
		if (!Strings.isNullOrEmpty(role_id)){
			roleInfo = roleInfoService.getRoleInfoById(role_id);
		}
		resultMap.put("roleInfo", roleInfo);
		return new ModelAndView("authManage/roleEdit", resultMap);
	}
	/**
	 * 保存增改角色
	 * 
	 * @param request
	 * @param response
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveRoleInfo.do")
	public ModelAndView saveRoleInfo(HttpServletRequest request,
			HttpServletResponse response, RoleInfo roleInfo) throws Exception {
		request.setCharacterEncoding("UTF-8");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String msg = checkForm(roleInfo);
		if("".equals(msg)){
			String role_id = ChunQiuTool.convertNull(roleInfo.getId());
			if (Strings.isNullOrEmpty(role_id)){ 
				//保存新增
				roleInfoService.addRoleInfo(roleInfo);
				msg = "addSuccess";
			}else {        
			   //保存更新
				roleInfoService.updateRoleInfo(roleInfo);
				msg = "updateSuccess";
			}
		}
		resultMap.put("msg", msg);
		JsonUtil.outputJson(resultMap,response);
		return null;
	}
	/**
	 * 后台验证
	 * 
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	public String checkForm(RoleInfo roleInfo) throws Exception {
		String error = "";
		if ("".equals(ChunQiuTool.convertNull(roleInfo.getName()))) {
			error = "角色名称不能为空！";
		} else if (ChunQiuTool.convertNull(roleInfo.getMemo()).length() > 200) {
			error = "角色描述长度不能大于200！";
		} else {
			int count = roleInfoService.isExistRole(roleInfo);
			if (count > 0) {
				error = "该角色已存在";
			}
		}
		return error;
	}
	/**
	 * 删除角色
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteRoleInfo.do")
	public ModelAndView deleteRoleInfo(HttpServletRequest request,
			HttpServletResponse response, RoleInfo roleInfo) throws Exception {
		request.setCharacterEncoding("UTF-8");
		roleInfoService.deleteRoleById(roleInfo.getId());
		return null;
	}
	/**
	 * 重启删除过的角色
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/restartRoleInfo.do")
	public ModelAndView restartRoleInfo(HttpServletRequest request,
			HttpServletResponse response, RoleInfo roleInfo) throws Exception {
		request.setCharacterEncoding("UTF-8");
		roleInfoService.restartRoleById(roleInfo.getId());
		return null;
	}

	/**
	 * 分配角色权限
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/updateRoleResource.do")
	public void updateRoleResource(HttpServletRequest request,
			HttpServletResponse response, String roleId, String resourceIds) {
	}

	/**
	 * 获取角色权限
	 * 
	 * @param request
	 * @param response
	 * @param roleId
	 */
	@RequestMapping("/getRoleResource.do")
	public ModelAndView getRoleResource(HttpServletRequest request,
			HttpServletResponse response, String roleId) throws Exception{
		List<RoleInfo> resList = roleInfoService.getRoleResource(roleId);
		List<String> lstTree = new ArrayList<String>();  
		if (resList!=null && resList.size()>0){
			for(RoleInfo info:resList){
				lstTree.add(info.getResourceId());					
			}
		}
        JsonUtil.outputJson(lstTree, response); 
		return null;
	}
	/**
	 * 获取资源树
	 * 
	 * @param request
	 * @param response
	 * @param roleId
	 */
	@RequestMapping("/getTree.do")
	public ModelAndView getTree(HttpServletRequest request,
			HttpServletResponse response,ResourceInfoParam param) throws Exception{
		param.setStatus("1");
		List<ResourceInfo> resList = resourceInfoService.queryResourceByParam(param);
		List<String> lstTree = new ArrayList<String>();  
		String id = "";
		String parentId = "";
		String name = "";
		StringBuffer strTree;
		if (resList!=null && resList.size()>0){
			for(ResourceInfo info:resList){
				strTree = new StringBuffer();
				id = info.getId();
				parentId = info.getParentId();
				name = info.getName();
				strTree.append("{id:").append(id).append(",pId:").
								append(parentId).append(",name:\"").append(name).
									append("\",").append("open:true}");
				lstTree.add(strTree.toString());					
			}
		}
        JsonUtil.outputJson(lstTree, response);
        return null;
	}
	/**
	 * 获取角色资源
	 * 
	 * @param request
	 * @param response
	 * @param roleId
	 */
	@RequestMapping("/saveRoleResource.do")
	public ModelAndView saveRoleResource(HttpServletRequest request,
			HttpServletResponse response,RoleInfo roleInfo) throws Exception{
		Map<String,Object> mapResult=new HashMap<String, Object>();
		String roleId = ChunQiuTool.convertNull(request.getParameter("roleId"));
		String resIds = ChunQiuTool.convertNull(request.getParameter("resIds"));
		roleInfo.setId(roleId);
		roleInfo.setResourceId(resIds);
		String msg = "";
		try {
			roleInfoService.udpateRoleResource(roleInfo);
			msg = "success";
		} catch (Exception e) {
			msg = "error";
		}
		mapResult.put("msg", msg);
		JsonUtil.outputJson(mapResult, response);
		return null;
	}
}

